﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using SportsStore.Domain.Abstract;
using SportsStore.Domain.Entities;
using SportsStore.WebUI.Models;

namespace SportsStore.WebUI.Controllers
{
   // [Authorize]
    public class AdminController : Controller
    {
        private IProductRepository productRepository;
        private ICategoryRepository categoryRepository;

        public AdminController(IProductRepository pRepo, ICategoryRepository cRep)
        {
            productRepository = pRepo;
            categoryRepository = cRep;
        }

        //
        // GET: /Admin/

        public ActionResult Index(int categoryId=0)
        {
            AdminIndexViewModel viewModel = new AdminIndexViewModel {CategoryId = categoryId,Categories = categoryRepository.Categories};
            if (categoryId > 0) //get product by category
            {
                viewModel.Products = productRepository.Products.Where(p => p.CategoryId == categoryId);
                return View(viewModel);
            }
            else //get all product
            {
                viewModel.Products = productRepository.Products;
                return View(viewModel);    
            }
            
        }

        //
        // GET: /Admin/Details/5
        /*
        public ActionResult Details(int id)
        {
            return View();
        }
        */
        //
        // GET: /Admin/Create

        public ActionResult Create()
        {
            return View("Edit",new Product());
        } 

        //
        // POST: /Admin/Create

       /* [HttpPost]
        public ActionResult Create(FormCollection collection)
        {
            try
            {
                // TODO: Add insert logic here

                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }
        */
        //
        // GET: /Admin/Edit/5
 
        public ActionResult Edit(int productId)
        {
            Product product = productRepository.Products.FirstOrDefault(x => x.Id == productId);
            return View(product);
        }

        //
        // POST: /Admin/Edit/5

        [HttpPost]
        [ValidateInput(false)]
        public ActionResult Edit(Product product, HttpPostedFileBase image)
        {
            //Server.HtmlEncode(product.Description);
            if (ModelState.IsValid)
            {
                if (image != null)
                {
                    product.ImageMimeType = image.ContentType;
                    //product.ImageData = new byte[image.ContentLength];
                    string path = @"\Content\images\products\display" + product.Id + ".jpg";
                    image.SaveAs(path);
                    //image.InputStream.Read(product.ImageData, 0, image.ContentLength);
                }
                productRepository.SaveProduct(product);
                TempData["message"] = string.Format("{0} image has been saved", product.Name);
                return RedirectToAction("Index");
            }
            else
            {
                return View(product);
            }
        }
            
        

        //
        // GET: /Admin/Delete/5
 /*
        public ActionResult Delete(int id)
        {
            return View();
        }
*/
        //
        // POST: /Admin/Delete/5

        [HttpPost]
        public ActionResult Delete(int productId)
        {
            Product prod = productRepository.Products.FirstOrDefault(p => p.Id == productId);
            if (prod != null)
            {
                productRepository.DeleteProduct(prod);
                TempData["message"] = string.Format("{0} was deleted", prod.Name);
            }
            return RedirectToAction("Index");
        }

        #region Category
        public ViewResult CategoryIndex()
        {
            IEnumerable<Category> categories = categoryRepository.Categories.OrderBy(c=>c.Ranking);
            return View(categories);
        }

        [HttpPost]
        public ActionResult UpCategory(int categoryId)
        {
            Category currentCategory = categoryRepository.Categories.FirstOrDefault(c => c.Id == categoryId);
            byte currentCategoryRanking = currentCategory.Ranking;
            if (currentCategoryRanking > 1)
            {
                Category previousCategory =
                    categoryRepository.Categories.FirstOrDefault(c => c.Ranking == currentCategoryRanking-1);
                byte previousCategoryRanking = previousCategory.Ranking;
                currentCategory.Ranking = previousCategoryRanking;
                previousCategory.Ranking = currentCategoryRanking;
                categoryRepository.SaveCategory(currentCategory);
                categoryRepository.SaveCategory(previousCategory);
            }
            else
            {
                ModelState.AddModelError("Ranking","Already the first one in the ranking list");
                //for JS
            }
            return RedirectToAction("CategoryIndex");
        }

        [HttpPost]
        public ActionResult DownCategory(int categoryId)
        {
            Category currentCategory = categoryRepository.Categories.FirstOrDefault(c => c.Id == categoryId);
            byte currentCategoryRanking = currentCategory.Ranking;
            if (currentCategoryRanking < categoryRepository.Categories.Max(c=>c.Ranking))
            {
                Category nextCategory =
                    categoryRepository.Categories.FirstOrDefault(c => c.Ranking == currentCategoryRanking + 1);
                byte nextCategoryRanking = nextCategory.Ranking;
                currentCategory.Ranking = nextCategoryRanking;
                nextCategory.Ranking = currentCategoryRanking;
                categoryRepository.SaveCategory(currentCategory);
                categoryRepository.SaveCategory(nextCategory);
            }
            else
            {
                ModelState.AddModelError("Ranking", "Already the last one in the ranking list");
                //for JS
            }
            return RedirectToAction("CategoryIndex");
        }

        public ViewResult CreateCategory()
        {
            return null;
        }

        public ViewResult EditCategory(int categoryId)
        {
            Category category = categoryRepository.Categories.FirstOrDefault(c => c.Id == categoryId);
            return View(category);
        }

        [HttpPost]
        public ViewResult EditCategory(Category category)
        {
            return null;
        }
        #endregion

    }
}
